/*
 *  Copyright 2009 Jamie Gennis <jgennis@gmail.com>.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package com.google.code.gwt.remoteaction.group.client;

import com.google.code.gwt.remoteaction.client.ActionExecutionService;
import com.google.code.gwt.remoteaction.client.ActionExecutionServiceAsync;
import com.google.code.gwt.remoteaction.client.ActionProxy;
import com.google.code.gwt.remoteaction.group.client.Service.SomeException;
import com.google.gwt.core.client.GWT;
import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * 
 * @author jgennis
 */
public class GwtTestRemoteActionGroup extends GWTTestCase {

  static class Results {

    public int five = 0;

    public int eight = 0;
    
    public boolean exceptionThrown = false;
    
    public boolean groupFailed = false;
  }

  ActionExecutionServiceAsync actionExecutionService;

  ServiceAsync service;

  ActionGrouper actionGrouper;

  @Override
  public String getModuleName() {
    return "com.google.code.gwt.remoteaction.group.RemoteActionGroup";
  }

  @Override
  protected void gwtSetUp() throws Exception {
    actionExecutionService =
        (ActionExecutionServiceAsync) GWT.create(ActionExecutionService.class);
    service = (ServiceAsync) GWT.create(Service.class);
    GroupingActionFilter groupingActionFilter = new GroupingActionFilter();
    actionGrouper = groupingActionFilter;

    ((ActionProxy) service).setActionExecutor(groupingActionFilter);
    groupingActionFilter.setActionExecutor(actionExecutionService);
  }

  public void testSuccess() {
    final Results results = new Results();
    ActionGroup actionGroup = actionGrouper.beginActionGroup(new AsyncCallback<Void>() {

      @Override
      public void onSuccess(Void result) {
        assertEquals(5, results.five);
        assertEquals(8, results.eight);
        finishTest();
      }

      @Override
      public void onFailure(Throwable caught) {
        throw new RuntimeException(caught);
      }
    });
    service.getFive(new AsyncCallback<Integer>() {

      @Override
      public void onSuccess(Integer result) {
        results.five = result;
      }

      @Override
      public void onFailure(Throwable caught) {
        throw new RuntimeException(caught);
      }
    });
    service.getEight(new AsyncCallback<Integer>() {

      @Override
      public void onSuccess(Integer result) {
        results.eight = result;
      }
      
      @Override
      public void onFailure(Throwable caught) {
        throw new RuntimeException(caught);
      }
    });
    assertEquals(2, actionGroup.numActions);
    actionGrouper.endActionGroup(actionGroup);
    delayTestFinish(10000);
  }
  
  public void testFailure() {
    final Results results = new Results();
    ActionGroup actionGroup = actionGrouper.beginActionGroup(new AsyncCallback<Void>() {

      @Override
      public void onSuccess(Void result) {
        fail("unexpected action group success");
      }

      @Override
      public void onFailure(Throwable caught) {
        try {
          throw caught;
        } catch (SomeException ex) {
          results.groupFailed = true;
          if (results.exceptionThrown)
            finishTest();
        } catch (Throwable t) {
          throw new RuntimeException(t);
        }
      }
    });
    service.getFive(new AsyncCallback<Integer>() {

      @Override
      public void onSuccess(Integer result) {
        results.five = result;
      }

      @Override
      public void onFailure(Throwable caught) {
        throw new RuntimeException(caught);
      }
    });
    service.throwException(new AsyncCallback<Void>() {

      @Override
      public void onSuccess(Void result) {
        fail("unexpected action success");
      }
      
      @Override
      public void onFailure(Throwable caught) {
        try {
          throw caught;
        } catch (SomeException ex) {
          results.exceptionThrown = true;
          if (results.groupFailed)
            finishTest();
        } catch (Throwable t) {
          throw new RuntimeException(t);
        }
      }
    });
    service.getEight(new AsyncCallback<Integer>() {

      @Override
      public void onSuccess(Integer result) {
        results.eight = result;
      }
      
      @Override
      public void onFailure(Throwable caught) {
        throw new RuntimeException(caught);
      }
    });
    assertEquals(3, actionGroup.numActions);
    actionGrouper.endActionGroup(actionGroup);
    delayTestFinish(10000);
  }
}
